<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-card>
        <el-input v-model="params.bookName" style="width: 200px" placeholder="请输入图书名称"></el-input>
        <el-button type="primary" style="margin-left: 10px" @click="findBySearch()">查询</el-button>
        <el-button type="warning" style="margin-left: 10px" @click="reset()">清空</el-button>
      </el-card>
    </div>
    <div>
      <el-card>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="序号" width="120"></el-table-column>
          <el-table-column prop="bookName" label="图书名称"></el-table-column>
          <el-table-column prop="userName" label="借阅人"></el-table-column>
          <el-table-column prop="time" label="借阅时间"></el-table-column>
          <el-table-column label="操作" align="center">
            <template v-slot="scope">
<!--              <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">-->
<!--                <el-button slot="reference" type="danger" style="margin-left: 5px" v-if="user.role === 'ADMIN'">删除</el-button>-->
<!--              </el-popconfirm>-->
              <el-button type="danger" @click="del(scope.row.id)" v-if="user.role === 'ADMIN'">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <div style="margin-top: 10px">
      <el-card>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.pageNum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next"
            :total="total">
        </el-pagination>
      </el-card>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      params: {
        bookName: '',
        author: '',
        pageNum: 1,
        pageSize: 5
      },
      tableData: [],
      total: 0,
      form: {},
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  // 页面加载的时候，做一些事情，在created里面
  created() {
    this.findBySearch();
  },
  // 定义一些页面上控件出发的事件调用的方法
  methods: {
    findBySearch() {
      request.get("/borrow/search", {
        params: this.params
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list;
          this.total = res.data.total;
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 5,
        bookName: '',
        phone: ''
      }
      this.findBySearch();
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.findBySearch();
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.findBySearch();
    },
    del(id) {
      this.$confirm('确定要删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request.delete("/borrow/" + id).then(res => {
          if (res.code === '200') {
            this.$message.success("删除成功");
            this.findBySearch();
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
        // 用户点击了取消按钮或者弹窗自动关闭
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }


  }
}
</script>